<?php
if(!defined('IN_STORE')) {
	exit('Acess Denied');
}

if(submitcheck('shoptagsubmit')) {
	require_once(B_ROOT.'./source/function/cache.func.php');
	$value = array();
	if(!empty($_POST['shoptag'])){
		foreach($_POST['shoptag'] as $id=>$name) {
			DB::query("UPDATE ".DB::table('shoptag')." SET `tagname`='$name' WHERE id='".$id."'");
		}
	}
	if(!empty($_POST['newshoptag'])) {
		foreach($_POST['newshoptag'] as $name) {
			$value[] = "('".$_SGLOBAL['panelinfo']['itemid']."', '".$name."')";
		}
		DB::query("INSERT INTO ".DB::table('shoptag')." (`shopid`, `tagname`) VALUES ".implode(",", $value));
	}
	updatesettingcache();
	cpmsg('update_success', '?action=shoptag');
}
if($_GET['op'] == 'del') {
	$id = intval($_GET['id']);
	if($id) {
		DB::query("DELETE FROM ".DB::table('shoptag')." WHERE id = '".$id."' AND shopid ='".$_SGLOBAL['panelinfo']['itemid']."'");
	}
	updatesettingcache();
	cpmsg('update_success', '?action=shoptag');
}

shownav('global', 'nav_shoptag');
showsubmenu('nav_shoptag');
showformheader('shoptag');
showtableheader('');
showsubtitle(array('id', 'shoptag_name', 'edit'));

$query = DB::query("SELECT * FROM ".DB::table('shoptag')." WHERE shopid = '".$_SGLOBAL['panelinfo']['itemid']."' ");
while($value = DB::fetch($query)) {
	$rowitem = array();
	$rowitem[] = $value['id'];
	$rowitem[] = '<input name="shoptag['.$value['id'].']" type="text" size="8" value="'.$value['tagname'].'" />';
	$rowitem[] = '[<a href="?action=shoptag&op=del&id='.$value['id'].'">'.lang('nav_del').'</a>]';
	showtablerow('', array(), $rowitem);
}
echo "
<script type=\"text/JavaScript\">
	var rowtypedata = [
		[ 
			[1, '', ''], 
			[1, '<input name=\"newshoptag[]\"  value=\"\"  type=\"text\"  class=\"txt\">', ''],
			[1, '', ''], 
		],
	];
</script>";
echo '<tr><td colspan="8"><div><a href="###" onclick="addrow(this,0)" class="addtr">'.$lang['tag_add'].'</a></div></td></tr>';
showsubmit('shoptagsubmit');
showtablefooter();
showformfooter();
bind_ajax_form();
?>
